﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using MockPrjTeam4.Entity;
using MockPrjTeam4.Common;

namespace MockPrjTeam4.DAO
{
    public class TypeOfBusinessDAO
    {
        public static TypeOfBusiness TypeOfBusinessObject(IDataRecord read)
        {
            return new TypeOfBusiness
            {
                BusinessId = Methods.ConvertNumber(read[TypeOfBusiness.FieldBusinessId].ToString()),
                BusinessName = Methods.ConvertString(read[TypeOfBusiness.FieldBusinessName].ToString()),
                SICCode = Methods.ConvertString(read[TypeOfBusiness.FieldSICCode].ToString())
            };
        }

        public static IList<TypeOfBusiness> GetListTypeOfBusiness()
        {
            IList<TypeOfBusiness> typeOfBusiness = new List<TypeOfBusiness>();
            string query = string.Format("SELECT * FROM BusinessTypes");
            IDataReader read = SqlHelper.ExecuteReader(query);
            while (read.Read())
            {
                typeOfBusiness.Add(TypeOfBusinessObject(read)); 
            }
            return typeOfBusiness;
        }

        public static TypeOfBusiness GetTypeOfBusinessById(int id)
        {
            string query = string.Format("SELECT * FROM BusinessTypes WHERE BusinessId = @TypeOfBusinessId");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@TypeOfBusinessId", id)
            };
            IDataReader read = SqlHelper.ExecuteReader(query, parameter);
            while (read.Read())
            {
                return TypeOfBusinessObject(read);
            }
            return null;
        }

        public static IList<TypeOfBusiness> Search(string firstName, string surname)
        {
            IList<TypeOfBusiness> typeOfBiz = new List<TypeOfBusiness>();
            string query = string.Format("SELECT * FROM BusinessTypes WHERE BusinessName LIKE '%'+@BizName+'%' AND " +
                "SICCode LIKE '%'+@SICCode+'%'");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@BizName", firstName),
                new SqlParameter("@SICCode", surname),
            };

            IDataReader read = SqlHelper.ExecuteReader(query, parameter);
            while (read.Read())
            {
                typeOfBiz.Add(TypeOfBusinessObject(read));
            }
            return typeOfBiz;
        }
    }
}